﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using PublicClass.HttpClient;
using S.Produce.Api.Application.Commands.DeliveryCommands;
using S.Produce.Domains.DTO;
using System.Security.Claims;

namespace S.Produce.Api.Controllers
{
    /// <summary>
    /// 种母分娩控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class DeliverysController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ServiceHttpClient serviceHttpClient;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="serviceHttpClient"></param>
        public DeliverysController(IMediator mediator, ServiceHttpClient serviceHttpClient)
        {
            this.mediator = mediator;
            this.serviceHttpClient = serviceHttpClient;
        }

        /// <summary>
        /// 添加种母分娩信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeliveryCreate(DeliveryCreateCommand command)
        {
            var UserId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            command.CreateBy = JsonConvert.DeserializeObject<UserNameDto>(await serviceHttpClient.Get("http://10.31.56.20:8092/api/StaffInfo/GetStaffName?StaffId=" + UserId)).Name;
            return Ok(await mediator.Send(command));
        }

        /// <summary>
        /// 删除分娩信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeliveryDelete([FromQuery] DeliveryDeleteCommand command)
        {
            return Ok(await mediator.Send(command));
        }

        /// <summary>
        /// 修改分娩信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> DeliverUpdate(DeliveryUpdateCommand command)
        {
            var UserId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            command.CreateBy = JsonConvert.DeserializeObject<UserNameDto>(await serviceHttpClient.Get("http://10.31.56.20:8092/api/StaffInfo/GetStaffName?StaffId=" + UserId)).Name;
            return Ok(await mediator.Send(command));
        }
    }
}
